package com.lb.poitl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

import lombok.Data;

/**
 * @author jzhang
 * @version 1.0
 * @date 2022/5/26 11:40
 */
public class PoiTlTest {
    public static void main(String[] args) throws IOException {
        List<String> imgs = new ArrayList<String>(){{
            add("D:\\data\\docbase\\merlionw\\report\\chart\\td165104459947930008\\db9529806ad04107be1c10fa93929eb.png");
            add("D:\\data\\docbase\\merlionw\\report\\chart\\td165104459947930008\\06731457e9b94a30802225cc67407701.png");
        }};


        TempData data = new TempData();
        User u = new User();
        u.setAge(10);
        data.setUser(u);

        User u2 = new User();
        u2.setName("张三");
        List<Account> a1 = new ArrayList<Account>(){{
            add(new Account(10000L, "苏州银行1"));
            add(new Account(10001L, "建设银行1"));
        }};
        u2.setAccountList(a1);

        User u3 = new User();
        u3.setName("李四");
        List<Account> a2 = new ArrayList<Account>(){{
            add(new Account(10002L, "中国银行"));
            add(new Account(10003L, "宁波银行"));
        }};
        u3.setAccountList(a2);

        List<User> userList = new ArrayList<User>() {{
            add(u2);
            add(u3);
        }};

        List<Student> studentList = new ArrayList<Student>(){{
            add(new Student("小美", "20", "一班", 98, imgs));
            add(new Student("小刘", "20", "二班", 90, imgs));
            add(new Student("小王", "22", "三班", 92, imgs));
            add(new Student("小明", "22", "四班", 92, imgs));
        }};

        List<Medicine> medicineList = new ArrayList<Medicine>(){{
            add(new Medicine("氯巴占", 0,"CYP2C19", "*1/*2/*3", "*1/*2", "代谢稍差，副作用风险较高","药物不良反应⻛险较⾼，建议加强⽤药监测", "2A"));
            add(new Medicine("阿司匹林", 1,"CYP2C19", "*1/*2/*17", "*1/*2", "卒中和⾎⼩板反应⻛险增加","药物不良反应⻛险较⾼，建议加强⽤药监测", "2B"));
            add(new Medicine("阿司匹林", 1,"HLA-DPB1", "HLA-DPB1*03:01:01", "阴性", "建议正常使⽤","药物不良反应⻛险较⾼，建议加强⽤药监测", "2B"));
            add(new Medicine("华法林", 2,"CYP2C9", "*1/*2/*3", "*1/*1", "代谢正常，建议正常使⽤","建议正常使⽤", "1A"));
            add(new Medicine("华法林", 2,"CYP2C9", "*1/*2/*3/*5/*6/*11", "*1/*1", "过度抗凝⻛险较低","建议正常使⽤", "2A"));
            add(new Medicine("华法林", 2,"CYP2C9", "*1/*2/*3", "*1/*1", "出⾎⻛险降低","建议正常使⽤", "2A"));
            add(new Medicine("丙戊酸", 3, "尿素循环相关基因", "基因外显区", "阴性", "未检测到丙戊酸相关尿素循环障碍基因的有害变异","建议正常使⽤", "-"));
        }};

        List<String> stringList = new ArrayList<String>(){{
            add("<strong>我的</strong>");
            add("<span color='#ff0000'>iPhone14</span>");
            add("已经预订。");
        }};

        List<List<Account>> accounts = new ArrayList<>();
        accounts.add(a1);
        accounts.add(a2);


        data.setName("从受检者外周血中提取DNA，采用多重连接探针扩增技术（MLPA）检测待检样本基因外显子缺失/重复变异，并以正常DNA作对照，数据采用Coffalyser软件分析。\n\n该方法无法检测基因点变异及微小缺失/重复。");
//        data.setAccountList(a2);
//        data.setUserList(userList);
//        data.setMoney(null);
//        data.setStudentList(studentList);
//        data.setMedicineList(medicineList);
//        data.setStringList(stringList);
//        data.setUser(u2);
//        data.setAccounts(accounts);
//        data.setImgUrl("D:\\data\\docbase\\merlionw\\report\\chart\\td165104459947930008\\e0608fc4d90d42618e007983f2dae5ef.png");
//        data.setImgs(imgs);
//        data.setAccountList(a2);
//        data.setIsShow(false);
//        data.setHtml("<strong>Kleefstra综合征2型</strong>（Kleefstra syndrome 2）[PMID:29069077]，" +
//                "该病是一种<i>常染色体显性遗传</i>性神经发育障碍疾病，其特征在于精神运动发育迟缓、智力障碍可变以及轻微的异质性特征。");
//        data.setHtml("<span style=\"color:#595757;font-family:&quot;font-size:14px;background-color:#FFFFFF;\">主诉及<span style=\"color:#FF0000;\">病史</span></span><span style=\"color:#595757;font-family:&quot;font-size:14px;background-color:#FFFFFF;\">主诉<strong>及<span style=\"color:rgb(255,0,0);\">病史</span></strong></span><span style=\"color:#595757;font-family:&quot;font-size:14px;background-color:#FFFFFF;\"><strong>主诉及病</strong>史</span><span style=\"color:#595757;font-family:&quot;font-size:14px;background-color:#FFFFFF;\">主诉及<span style=\"color:#FF0000;\">病史</span></span>");
        data.setHtml("根据《ACMG遗传变异分类标准与指南》，该变异符合“疑似良性变异”：PVS1_VeryStrong。<br>    PVS1：<p>very very</p><br>该基因关联疾病为常染色体显性遗传，患者该位点为杂合，合子类型可以解释患者患病。");
        LoopRowTableRenderPolicy loopRowTableRenderPolicy = new LoopRowTableRenderPolicy();
        Configure configure = Configure.builder()
                .bind("user.accountList", loopRowTableRenderPolicy)
                .bind("accountList", loopRowTableRenderPolicy)
//                .bind("=#this", loopRowTableRenderPolicy)
                .bind("html", new HtmlRenderPolicy())
                .bind("studentList", loopRowTableRenderPolicy)
                .bind("medicineList", new MergeTableRenderPolicy())
//                .bind("stringList", new HtmlRenderPolicy())
                .addPlugin('%', new ImagePolicy())
                .build();

        XWPFTemplate template = XWPFTemplate.compile("D:\\template.docx", configure).render(data);
        FileOutputStream out = new FileOutputStream("D:\\out_template.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

    @Data
    public static class TempData {
        private String name;
        private User user;
        private List<Account> accountList;
        private List<User> userList;
        private BigDecimal money;
        private String html;
        private List<Student> studentList;
        private List<Medicine> medicineList;
        private List<String> stringList;
        private List<List<Account>> accounts;
        private String imgUrl;
        private List<String> imgs;
        private Boolean isShow;
    }

    @Data
    public static class User {
        private int age;
        private String name;
        private List<Account> accountList;
    }

    @Data
    public static class Account {
        private Long id;
        private String bank;

        public Account(Long id, String bank) {
            this.id = id;
            this.bank = bank;
        }
    }

    @Data
    public static class Student {
        public Student(String name, String age, String classroom, int score, List<String> images) {
            this.name = name;
            this.age = age;
            this.classroom = classroom;
            this.score = score;
            this.images = images;
        }

        private String name;
        private String age;
        private String classroom;
        private int score;
        private List<String> images;
    }

    @Data
    public static class Medicine {
        public Medicine(String name,Integer order, String gene, String pos, String result, String testResultDescription, String medicineAdvice, String evidenceRank) {
            this.name = name;
            this.order = order;
            this.gene = gene;
            this.pos = pos;
            this.result = result;
            this.testResultDescription = testResultDescription;
            this.medicineAdvice = medicineAdvice;
            this.evidenceRank = evidenceRank;
        }

        private String name;
        private Integer order;
        private String gene;
        private String pos;
        private String result;
        private String testResultDescription;
        private String medicineAdvice;
        private String evidenceRank;
    }
}
